local api = require 'types.skill'.__index

function api:get_gold()
	return self.gold or 0
end

function api:get_lum()
	return self.lum or 0
end
function api:get_food()
	return self.food or 0
end


--[检查玩家能否购买]
function api:check_player(player)
    local bool = not self.supply and true or (self.supply == player) 
    if bool == true then 
        bool = not self.team and true or (self.team == player.team) 
    end 
    --新增
    if bool == true then 
        local key = ('禁止购买%s'):format(player.id)
        bool = self:get_data(key)~=true
    end 
    return bool 
end 

ac.skill('商品'){
    is_shop_item = true,
    set_item = function(self,name)
        local item = ac.item.new(name)
        if item==nil then
            print('没有可出售的物品',name)
            return
        end
        self.bind_item = item
        self.gold = item.gold or item.goldcost
        self.lum = item.lum or item.lumbercost
        self.food = item.food or item.foodcost

        self:set_art(item.art)
        
        function self:title()
            return format_obj(item,item.title or item.name)
        end

        function self:tip()
            return format_obj(item,item.tip)
        end


        if item.stockMax then
            --使用充能模拟并且显示数字和冷却
            self.cooldown_mode = 1
            self.show_stack = 1
            self.show_charge = 1

            --商店数量
            self.charge_max_stack = item.stockMax

            --商店进货时间
            self.charge_cool = item.stockRegen or 1
            --商店初始数量
            if item.stockStart then
                self.spell_stack = 0
                self:set_cooling(item.stockStart)
            else
                self.spell_stack = self.charge_max_stack
            end
            self:set_stack(self.spell_stack)
        end
    end,
}

--[给单位添加商品]
function ac.unit.__index:add_sell(name,id)
    local skill = self:add_skill('商品','英雄',id)
    skill:set_item(name)
    
    return skill
end


--商店初始化
function ac.unit.__index:add_sells(data)
    local player = self.owner
    for a=1,12 do
        local name = data[a]
        if name and name~='~' then
            if not player:is_online() then
                self:add_sell(name, a)
            else
                self:add_sell(name,a)
            end
        end
    end
    player:notify('玩家-商店创建',player,self)
end


ac.game:event '商店-购买商品检查'(function(_,shop,skill,buyer)
    if not buyer then
        return false
    end
    if skill.removed then
        return false
    end
    local player = buyer.owner
    if not buyer then
        return false
    end
    if skill:check_player(player)~=true then
        return false
    end
    --剩余数量检查
    if skill.cooldown_mode==1 and skill:get_stack()<=0 then
        return false
    end
    local ok, reason = skill:notify("on_check", unit, player)
    if ok == false then 
        return false, reason
    end
    if buyer:dispatch('商店-购买商品判定',buyer,skill)==false then
        return false
    end


    --费用检查
    local gold = skill:get_gold(player)
    local lum = skill:get_lum(player)
    local food = skill:get_food(player)
    if player:get('金币')<gold then
        show_skill_tip(player,skill.slotid,'金币')
        return false,'金币不够'
    end
    if player:get('木材')<lum then
        show_skill_tip(player,skill.slotid,'木材')
        return false,'木材不够'
    end
    if player:get('杀敌')<food then
        show_skill_tip(player,skill.slotid,'杀敌')
        return false,'杀敌不够'
    end

    --有真实物品
    local data = skill.bind_item
    if data then
        if data.empty_item or skill.empty_item or skill['强制购买'] then
            return true
        end
        --神符
        if data:is_class('powerup') then
            return true
        end
        --叠加
        if data.auto_merge then
            if buyer:find_item(name) then
                return true
            end
        end
        if #buyer:get_items() == 6 then
            return false, '物品栏已满'
        end
    end
    return true
end)

ac.game:event '商店-发起商品购买'(function(_,shop,skill,buyer,player)
    local player = player or buyer.owner
    if shop:dispatch('商店-购买商品检查',shop,skill,buyer)==false then
        return
    end
    --扣除费用
    local gold = skill:get_gold(player)
    local lum = skill:get_lum(player)
    local food = skill:get_food(player)
    player:add('金币',-gold)
    player:add('木材',-lum)
    player:add('杀敌',-food)
    
    if skill.cooldown_mode==1 then
        skill:add_stack(-1)
    else
        skill:active_cd()
    end
    
    --真实物品
    local item = skill.bind_item
    if item then
        item:build()
        item:give(buyer)
        skill:set_item(item.name)
        show_skill_tip(player,skill,'刷新')
    end
    local item = item or skill
    if item.on_sell then
        item:on_sell(buyer,player)
    end
    shop:notify("商店-出售商品", shop, item, buyer, player)
    player:notify("玩家-购买商品", player, item, buyer, shop)
    shop:notify('控制台-刷新技能显示', shop )
    return true
end)



ac.sync:event '同步-购买商品'(function(_,player,sp_handle,slotid)
    local shop = ac.unit(sp_handle)
    if shop then
        local unit = player:get_store_target(shop)
        local skill = shop:find_skill(slotid,'英雄')
        if unit and skill then
            shop:notify('商店-发起商品购买',shop,skill,unit,player)
        end
    end
end)


ac.game:event '单位-发动技能效果'(function(_,unit,skill)
    if skill.on_sell then
        local player = unit.owner
        local hero = player.hero
        if hero then
            if unit:notify('商店-发起商品购买',unit,skill,hero,player)~=true then
                skill:set_cd(0)
            end
        end
    end
end)
